/**
 *  assistant.vala
 *
 *  Copyright (c) 2007 Marcelo Lira dos Santos <setanta@gmail.com>
 *                2007 INdT (Instituto Nokia de Technologia)
 *
 *  Author: Marcelo Lira dos Santos <setanta@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 *  USA
 */

using GLib;
using Gtk;

public class AssistantDemo : GLib.Object
{
    Assistant assistant;

    private void
    on_assistant_apply (Assistant assistant)//GtkWidget *widget, gpointer data)
    {
        /* Apply here changes, this is a fictional
           example, so we just do nothing here */
        stdout.printf("on_assistant_apply\n");
    }

    private void
    on_assistant_close_cancel (Assistant assistant, pointer data)
    {
        stdout.printf("on_assistant_close_cancel\n");
        //GtkWidget **assistant = (GtkWidget **) data;

        assistant.destroy();
        assistant = null;
    }

    private void
    on_assistant_prepare (Assistant assistant)//GtkWidget *widget, GtkWidget *page, gpointer data)
    {
        stdout.printf("on_assistant_prepare\n");
        int current_page = assistant.get_current_page();
        int n_pages = assistant.get_n_pages();

        //title = g_strdup_printf ("Sample assistant (%d of %d)", current_page + 1, n_pages);
        string title = "Sample assistant";
        ((Window) assistant).title = title;
        //g_free (title);
    }

    private void
    on_entry_changed (Editable entry, pointer user_data)
    {
        Assistant assistant = (Assistant) user_data;
        int page_number = assistant.get_current_page();
        Widget current_page = assistant.get_nth_page(page_number);
        string text = ((Entry) entry).get_text();

        if (text != null && text != "")
            assistant.set_page_complete(current_page, true);
        else
            assistant.set_page_complete(current_page, false);
    }


    private void
    create_page1 (Assistant assistant)
    {
        HBox box = new HBox(false, 12);
        box.border_width = 12;

        Label label = new Label("You must fill out this entry to continue:");
        box.pack_start(label, false, false, 0);

        Entry entry = new Entry();
        box.pack_start(entry, true, true, 0);
        entry.changed += on_entry_changed;//(entry, assistant);

        box.show_all();
        assistant.append_page(box);
        assistant.set_page_title(box, "Page 1");
        assistant.set_page_type(box, AssistantPageType.INTRO);

        Gdk.Pixbuf pixbuf = assistant.render_icon(STOCK_DIALOG_INFO,
                                                  IconSize.DIALOG,
                                                  null);
        assistant.set_page_header_image(box, pixbuf);
        pixbuf.unref();
    }

    private void
    create_page2 (Assistant assistant)
    {
        VBox box = new VBox(false, 12);
        box.border_width = 12;

        CheckButton checkbutton = new CheckButton.with_label("This is optional data, you may continue even if you do not check this");
        box.pack_start(checkbutton, false, false, 0);

        box.show_all();
        assistant.append_page(box);
        assistant.set_page_complete(box, true);
        assistant.set_page_title(box, "Page 2");

        Gdk.Pixbuf pixbuf = assistant.render_icon(STOCK_DIALOG_INFO,
                                                  IconSize.DIALOG,
                                                  null);
        assistant.set_page_header_image(box, pixbuf);
        pixbuf.unref();
    }

    private void
    create_page3 (Assistant assistant)
    {
        Label label = new Label("This is a confirmation page, press 'Apply' to apply changes");

        label.show();
        assistant.append_page(label);
        assistant.set_page_type(label, AssistantPageType.CONFIRM);
        assistant.set_page_complete(label, true);
        assistant.set_page_title(label, "Confirmation");

        Gdk.Pixbuf pixbuf = assistant.render_icon(STOCK_DIALOG_INFO,
                                                  IconSize.DIALOG,
                                                  null);
        assistant.set_page_header_image(label, pixbuf);
        pixbuf.unref();
    }

    public Assistant do_assistant ()
    {
        Assistant assistant = new Assistant();

        assistant.title = "Assistant";
        assistant.set_default_size(-1, 300);

        create_page1(assistant);
        create_page2(assistant);
        create_page3(assistant);

        assistant.destroy += Gtk.main_quit;
        assistant.cancel += on_assistant_close_cancel;
        //button1.clicked += btn => { on_button1_clicked(button1, "test"); };
        assistant.close += on_assistant_close_cancel;
        assistant.apply += on_assistant_apply;
        assistant.prepare += on_assistant_prepare;

        return assistant;
    }

    static int main (string[] args) {
        Gtk.init (out args);

        AssistantDemo ad = new AssistantDemo ();
        Assistant win = ad.do_assistant ();

        try {
            win.set_default_icon_from_file ("v-icon.png");
        } catch (GLib.Error error) {
            critical (error.message);
        }
        win.show_all ();

        Gtk.main ();

        return 0;
    }
}

